#!/bin/bash

echo "env: export FORCE=true"

export FORCE=true



cat <<EOF
build docker image
AND run container (api server)
EOF

# cat <<EOF
# apt update
# apt intall python3-pip
# # python
#  pip install pyexecjs
# # pyexecjs need js runtime
# apt install nodejs  # version==12 is OK

# EOF


if [ -z "$1" ] || [ ! -d "$1" ];then
        echo '[err] need arg1 , and arg1 == dir'
        exit
fi

# setting
HOUR=3
MIN=6


####################
# name="tvbox_iptv"
src="$(dirname "$0")"
name="$(basename "$(cd "$src";pwd)")"

pwd


echo "[i] name=$name"

###############
dst_dir="$1"
dst="$dst_dir/$name"


df -h "$dst_dir"

read -t 1 -p "continue in 5 sec, press ctr+c if need exit "
echo
read -t 5 -p "continue in 5 sec, press ctr+c if need exit "
echo

mkdir "$dst"

if [ ! -d "$dst" ];then
        echo "[E] dir not exits : $dst, please make it"
        exit 1
fi

echo  "[i] install to $dst"


## 不需要这个 docker 的服务了, 已经使用 golang 重新写了 bili. douyu等的 js解析
# cp -r "$src/server.d" "$dst/"
# cp -r "$src/docker" "$dst/"


## cron
n="cron_job.sh"
cron_job="$src/$n"
echo "$cron_job"
cp "$cron_job" "$dst/"
cp "$src/"_* "$dst/"

# todo /etc/cron/root
cron_cmd="bash '$cron_job'"
if ! grep -F "$cron_cmd" /etc/crontab;then
  echo "[i] append to crontab"
  {
  cat <<EOF
# 0 0 * * * $USER bash xx
$MIN $HOUR * * * $USER  $cron_cmd

EOF

  } | sudo tee -a /etc/crontab

fi

bash "$cron_job" clone
# sudo bash "$dst/docker/run.sh"

ls -lF "$dst"
echo "dst=$dst"

